<?php

namespace Have\Controller;

use Have\Api\UserApi;
use Think\Verify;
class UserController extends HaveController {
    public function index() {
        $this->meta_title = '用户';
        $this->display();
    }
    
    public function updatepass(){
        $this->meta_title = '用户';
        $this->display();
    }
    
    public function auth_manager() {
        $this->meta_title = '用户';
        if (USER_AUTH != 0) {
            $this->redirect('Index/index');
        }
        $this->display();
    }
    
    public function adduser() {
        if (USER_AUTH != 0 && USER_AUTH != 1)
            $this->redirect('Index/index');
        $username = I('post.username');
        $password = I('post.password');
        $repassword = I('post.repassword');
        $usertype = I('post.select');
        $verify = I('post.verify');
        $email = I('post.email');
        empty($username) && $this->error('请输入昵称');
        empty($password) && $this->error('请输入密码');
        empty($repassword) && $this->error('请输入确认密码');
        empty($email) && $email = '';
        
        if ($password !== $repassword) {
            $this->error('您输入的密码与确认密码不一致');
        }
        if (!check_verify($verify)) {
            $this->error('验证码输入错误！');
        }
     
        $User = new UserApi();
        $uid= $User->register($username, $password, $email, intval($usertype));
        if ($uid > 0) {
            $this->success('添加用户成功');
        } else {
            $this->error($this->showRegError($uid));
        }
    }
    
    public function updatename(){
        $this->meta_title = '用户';
        $this->display();
    }
    
    public function submitname() {
        $username = I('post.username');
        $password = I('post.password');
        empty($username) && $this->error('请输入昵称');
        empty($password) && $this->error('请输入密码');
        
        $User = new UserApi();
        $id = $User->login(UID, $password, 4);
        ($id == -2) && $this->error('密码不正确');
        
        $data = $User->create(array('username'=>$username));
        if (!$data) {
            $this->error($User->getError());
        }
        
        $res = $User->where(array('id'=>$id))->save($data);
        
        if ($res) {
            $user = session('user_auth');
            $user['username'] = $data['username'];
            session('user_auth', $user);
            session('user_auth_sign', data_auth_sign($user));
            $this->success('修改用户名成功！');
        } else {
            $this->error('修改用户名失败！');
        }
        
    }
    
    public function submitpass() {
        $password = I('post.old');
        empty($password) && $this->error('请输入原密码');
        $data['password'] = I('post.password');
        empty($data['password']) && $this->error('请输入新密码');
        $repassword = I('post.repassword');
        empty($repassword) && $this->error('请输入确认密码');
        
        if ($data['password'] !== $repassword) {
            $this->error('您输入的新密码与确认密码不一致');
        }

        $api = new UserApi();
        $res = $api->updateInfo(UID, $password, $data);
        if ($res['status']) {
            $this->success('修改密码成功！');
        }else{
            $this->error($res['info']);
        }
    }
    
    private function showRegError($code = 0){
        switch ($code) {
            case -1:  $error = '用户名长度必须在16个字符以内！'; break;
            case -2:  $error = '用户名被禁止注册！'; break;
            case -3:  $error = '用户名被占用！'; break;
            case -4:  $error = '密码长度必须在6-30个字符之间！'; break;
            case -5:  $error = '邮箱格式不正确！'; break;
            case -6:  $error = '邮箱长度必须在1-32个字符之间！'; break;
            case -7:  $error = '邮箱被禁止注册！'; break;
            case -8:  $error = '邮箱被占用！'; break;
            case -9:  $error = '手机格式不正确！'; break;
            case -10: $error = '手机被禁止注册！'; break;
            case -11: $error = '手机号被占用！'; break;
            default:  $error = $code;
        }
        return $error;
    }
}

?>
